package com.hl.wuyeguanliIDEA.web;

import com.hl.wuyeguanliIDEA.pojo.UrgentNotice;
import com.hl.wuyeguanliIDEA.service.UrgentNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/urgentNotices")
public class UrgentNoticeController {

    @Autowired
    private UrgentNoticeService urgentNoticeService;

    // 查询所有记录
    @GetMapping("/selectAll")
    public ResponseEntity<List<UrgentNotice>> selectAll() {
        List<UrgentNotice> urgentNotices = urgentNoticeService.selectAll();
        return new ResponseEntity<>(urgentNotices, HttpStatus.OK);
    }

    // 根据主键查询记录
    @GetMapping("/{id}")
    public ResponseEntity<UrgentNotice> selectByPrimaryKey(@PathVariable Long id) {
        Optional<UrgentNotice> urgentNotice = urgentNoticeService.selectByPrimaryKey(id);
        return urgentNotice.map(notice -> new ResponseEntity<>(notice, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    // 插入记录
    @PostMapping("/add")
    public ResponseEntity<UrgentNotice> add(@RequestBody UrgentNotice record) {
        int result = urgentNoticeService.add(record);
        if (result > 0) {
            return new ResponseEntity<>(record, HttpStatus.CREATED);
        } else {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 更新记录
    @PutMapping("/update")
    public ResponseEntity<Void> update(@RequestBody UrgentNotice record) {
        int result = urgentNoticeService.update(record);
        if (result > 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    // 删除记录
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        int result = urgentNoticeService.delete(id);
        if (result > 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
}